package com.cn.medicalcare.controller.backstageservlet.news;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.News;
import com.cn.medicalcare.service.NewsService;
import com.cn.medicalcare.service.serviceimpl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/news/updatenews.json")
public class NewsUpdateServlet extends HttpServlet {

    private NewsService newsService = NewsServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String category_id = req.getParameter("category_id");
        String title = req.getParameter("title");
        String summary = req.getParameter("summary");
        String newsdate = req.getParameter("newsdate");
        String survival = req.getParameter("survival");
        String istop = req.getParameter("istop");

        News news = new News();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            news.setId(Integer.valueOf(id)).setSurvival(Integer.valueOf(survival)).setIstop(Integer.valueOf(istop))
                    .setNewsdate(NewsServiceImpl.SDF.format(NewsServiceImpl.SDF.parse(newsdate))).setCategory_id(Integer.valueOf(category_id)).setTitle(title).setSummary(summary);
            if (newsService.updateNews(news)) {
                map.put("status", 200);
            } else {
                map.put("status", 500);
            }
        } catch (Exception e) {
            map.put("status", 400);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
